package controllers;


import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;

import controllers.Search.TextEntry;

import models.User;
import models.aaa.File;
import models.re.AAAContext;
import models.re.Re;
import models.re.aaa.AAAAlgorithm;
import models.re.realData.RealData;
import models.re.simulatedData.SimulatedDataOne;
import models.re.simulatedData.TestPerformances;
import play.Logger;
import play.data.Form;
import play.db.jpa.JPA;
import play.db.jpa.Transactional;
import play.mvc.Result;

public class Testing extends Application {
	
    @Transactional
    public static Result simulatedData(int dim) {
    	if(Authentication.isAdmin()) {
    		SimulatedDataOne.GenerateDatas(dim);
    		return ok("Simulated");
    	}
    	return forbidden();
    }

    @SuppressWarnings("deprecation")
	@Transactional
    public static Result createAAAGraph(String arg) {
    	if(Authentication.isAdmin()) {
	    	String[] args={arg};
	    	try {
				AAAAlgorithm.main(args);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    	return ok("AAA Graph created");
    	}
    	return forbidden();
    }
    
    @Transactional
    public static Result perfCurve() {
    	if(Authentication.isAdmin()) {
    		TestPerformances.perfCurve();
    		return ok("perfCurve");
    	}
    	return forbidden();
    }
    
    @Transactional
    public static Result perf() {
    	if(Authentication.isAdmin()) {
    		TestPerformances.perf();
    		return ok("perf");
    	}
		return forbidden();
    }
    
    @Transactional
    public static Result realData() {
    	if(Authentication.isAdmin()) {
	    	RealData.test();
	    	return ok("This is real");
    	}
		return forbidden();
    }
    
	@Transactional
    public static Result anonymiseData(String dataset) {
		if(Authentication.isAdmin()) {
	    	Session session = (Session) JPA.em().getDelegate();
	    	StatelessSession stateless = session.getSessionFactory().openStatelessSession();
	    	Transaction tx = stateless.beginTransaction();
	    	String sql = "SELECT e FROM Asset e WHERE e.dataset = ? ";
	        org.hibernate.Query query = stateless.createQuery(sql);
	        query.setParameter(0, dataset);
	        ScrollableResults results = query.scroll();
	        int count=0;
	        while ( results.next() ) {
	            models.aaa.Asset asset = (models.aaa.Asset) results.get(0);
	            asset.anonymise();
	            stateless.update(asset);
	            if ( ++count % 20 == 0 ) {
	                //flush a batch of updates and release memory:
	                session.flush();
	                session.clear();
	            }
	        }
	        Logger.info("Anonymised data: "+count);
	        tx.commit();
	        stateless.close();
	    	return ok("Dataset "+dataset+" anonymised");
		}
		return forbidden();
    }
	
    public static Result checkMoreLikeThis() { 	
    	if(Authentication.isAdmin()) {
	        RealData rd = new RealData();
	        rd.computeAllSimilarities(16);
	        return ok("Checked");
    	}
		return forbidden();
    }
    
    public static Result paralAAA(int threads) {
    	if(Authentication.isAdmin()) {
	    	Re.computeParallelAAA(threads);
	    	return ok("weee");
    	}
		return forbidden();
    }
    
    @Transactional
    public static Result getRec() {
    	if(Authentication.isAdmin()) {
	    	Form<TextEntry> form = play.data.Form.form(TextEntry.class).bindFromRequest();
	    	return ok(views.html.search.index.render(form, Re.getRecommendation(new AAAContext("d4e66009d4b5ab88133702f5d71b6622_26386"), 10), 10));
    	}
		return forbidden();
    }
    
    @Transactional
    public static Result tikaTest() {
    	if(Authentication.isAdmin()) {
	    	File.cleanFolder();
	    	return ok("Tika !");
    	}
		return forbidden();
    }
    
    @Transactional
    public static Result Login() {
    	if(Authentication.isAdmin()) {
	    	if(Application.getUser()==User.find.byName("admin")) return ok("Access");
	    	else return ok(""+Application.getUser().getName());
    	}
		return forbidden();
    }

}
